package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.os.Bundle;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.List;

import flc.ast.AppConfig;
import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.LatestAdapter;
import flc.ast.adapter.NpcAdapter;
import flc.ast.databinding.ActivityNpcBinding;
import stark.common.api.StkResApi;
import stark.common.base.IReqRetCallback;
import stark.common.basic.event.EventStatProxy;
import stark.common.bean.StkResBean;

public class NpcActivity extends BaseAc<ActivityNpcBinding> {

    private NpcAdapter npcAdapter;
    private int page = 1;
    private int refreshTime = 200;

    @Override
    protected int onCreate() {
        return R.layout.activity_npc;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);

        mDataBinding.ivNpcBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.rvNpcList.setLayoutManager(new LinearLayoutManager(mContext));
        npcAdapter = new NpcAdapter();
        mDataBinding.rvNpcList.setAdapter(npcAdapter);
        npcAdapter.addChildClickViewIds(R.id.tvNpcItemDetail);
        npcAdapter.setOnItemChildClickListener(this);

        mDataBinding.refreshNpcItem.setRefreshHeader(new ClassicsHeader(mContext));
        mDataBinding.refreshNpcItem.setRefreshFooter(new ClassicsFooter(mContext));
        mDataBinding.refreshNpcItem.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                getData();
                mDataBinding.refreshNpcItem.finishLoadMore(refreshTime);
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                getData();
                mDataBinding.refreshNpcItem.finishRefresh(refreshTime);
            }
        });
    }

    @Override
    public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
        GuideDetailActivity.stkResBean = npcAdapter.getItem(position);
        startActivity(GuideDetailActivity.class);
    }

    @Override
    protected void initData() {
        getData();
    }

    private void getData() {
        StkResApi.getTagResourceList(this, AppConfig.NPC_DATA, StkResApi.createParamMap(0, 10), false, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        if (page == 1) {
                            npcAdapter.setList(result);
                        } else {
                            npcAdapter.addData(result);
                        }
                    }
                }
            }
        });
    }

}